package com.service.impl;

import com.entity.BGoods;
import com.entity.table.FCartGoodsTableDef;
import com.entity.table.FCartTableDef;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.entity.FCart;
import com.mapper.FCartMapper;
import com.service.FCartGoodsService;
import com.service.FCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 *  服务层实现。
 *
 * @author admin
 * @since 2024-06-03
 */
@Service
public class FCartServiceImpl extends ServiceImpl<FCartMapper, FCart> implements FCartService {
    @Autowired
    FCartGoodsService fCartGoodsService;
    public FCart getUserCartInfo(int userid){
        QueryWrapper query = QueryWrapper.create().where(FCartTableDef.F_CART.USER_ID.eq(userid));
        FCart fCart=this.getMapper().selectOneWithRelationsByQuery(query);
        if (fCart!=null){
            for (BGoods good : fCart.getGoods()){
                fCartGoodsService.getTotalPriceByGood(fCart.getId(),good);
            }
            BigDecimal totaPrice = fCart.getGoods().stream().map(BGoods::getTotalPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
            fCart.setTotalPrice(totaPrice);
        }
        return fCart;
    }
}
